package com.shine.app.widget.rangebar;

import android.content.Context;
import android.content.res.Resources;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.util.TypedValue;

@SuppressWarnings("unused")
public class Thumb {

	private static final float MINIMUM_TARGET_RADIUS_DP = 24;
	private static final float DEFAULT_THUMB_RADIUS_DP = 14;
	private static final int DEFAULT_THUMB_COLOR_NORMAL = Color.RED;
	private static final int DEFAULT_THUMB_COLOR_PRESSED = Color.RED;
	private final float targetRadiusPx;
	private final Bitmap imageNormal;
	private final Bitmap imagePressed;
	private final float halfWidthNormal;
	private final float halfHeightNormal;
	private final float halfWidthPressed;
	private final float halfHeightPressed;
	private boolean isPressed = false;
	private final float y;
	private float x;
	private Paint paintNormal;
	private Paint paintPressed;
	private float thumbRadiusPx;
	private boolean useBitmap;
	private int thumbColorNormal;
	private int thumbColorPressed;

	public Thumb(Context context, float y, int thumbColorNormal,
			int thumbColorPressed, float thumbRadiusDP, int thumbImageNormal,
			int thumbImagePressed) {
		final Resources res = context.getResources();
		this.imageNormal = BitmapFactory.decodeResource(res, thumbImageNormal);
		this.imagePressed = BitmapFactory
				.decodeResource(res, thumbImagePressed);
		if (thumbRadiusDP == -1 && thumbColorNormal == -1
				&& thumbColorPressed == -1) {
			this.useBitmap = true;
		} else {
			this.useBitmap = false;
			if (thumbRadiusDP == -1) {
				this.thumbRadiusPx = TypedValue.applyDimension(
						TypedValue.COMPLEX_UNIT_DIP, DEFAULT_THUMB_RADIUS_DP,
						res.getDisplayMetrics());
			} else {
				this.thumbRadiusPx = TypedValue.applyDimension(
						TypedValue.COMPLEX_UNIT_DIP, thumbRadiusDP,
						res.getDisplayMetrics());
			}
			if (thumbColorNormal == -1) {
				this.thumbColorNormal = DEFAULT_THUMB_COLOR_NORMAL;
			} else {
				this.thumbColorNormal = thumbColorNormal;
			}
			if (thumbColorPressed == -1) {
				this.thumbColorPressed = DEFAULT_THUMB_COLOR_PRESSED;
			} else {
				this.thumbColorPressed = thumbColorPressed;
			}
			this.paintNormal = new Paint();
			this.paintNormal.setColor(thumbColorNormal);
			this.paintNormal.setAntiAlias(true);
			this.paintPressed = new Paint();
			this.paintPressed.setColor(thumbColorPressed);
			this.paintPressed.setAntiAlias(true);
		}
		this.halfWidthNormal = imageNormal.getWidth() / 2f;
		this.halfHeightNormal = imageNormal.getHeight() / 2f;
		this.halfWidthPressed = imagePressed.getWidth() / 2f;
		this.halfHeightPressed = imagePressed.getHeight() / 2f;
		int targetRadius = (int) Math.max(MINIMUM_TARGET_RADIUS_DP,
				thumbRadiusDP);
		targetRadiusPx = TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP,
				targetRadius, res.getDisplayMetrics());
		this.x = halfWidthNormal;
		this.y = y;
	}

	float getHalfWidth() {
		return halfWidthNormal;
	}

	float getHalfHeight() {
		return halfHeightNormal;
	}

	void setX(float x) {
		this.x = x;
	}

	float getX() {
		return x;
	}

	boolean isPressed() {
		return isPressed;
	}

	void press() {
		this.isPressed = true;
	}

	void release() {
		this.isPressed = false;
	}

	boolean isInTargetZone(float a, float b) {
		if (Math.abs(a - x) <= targetRadiusPx
				&& Math.abs(b - y) <= targetRadiusPx) {
			return true;
		}
		return false;
	}

	void draw(Canvas canvas) {
		if (useBitmap) {
			final Bitmap bitmap = (isPressed) ? imagePressed : imageNormal;
			if (isPressed) {
				final float topPressed = y - halfHeightPressed;
				final float leftPressed = x - halfWidthPressed;
				canvas.drawBitmap(bitmap, leftPressed, topPressed, null);
			} else {
				final float topNormal = y - halfHeightNormal;
				final float leftNormal = x - halfWidthNormal;
				canvas.drawBitmap(bitmap, leftNormal, topNormal, null);
			}
		} else {
			if (isPressed) {
				canvas.drawCircle(x, y, thumbRadiusPx, paintPressed);
			} else {
				canvas.drawCircle(x, y, thumbRadiusPx, paintNormal);
			}
		}
	}
}
